﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VOD.Models;
using VOD.ViewModels;
using System.Data;

namespace VOD.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class SiteConfigController : Controller
    {
        VODEntities db = new VODEntities();
        //
        // GET: /SiteConfig/
        public ActionResult Index()
        {
            var welcomeInfo = new AdminWelcomeInfo();
            welcomeInfo.User = HttpContext.User.Identity.Name;
            welcomeInfo.SiteCount = db.SiteCounts.Count();
            welcomeInfo.VideosCount = db.Videos.Count();
            welcomeInfo.VideoFilesCount = db.VideoUrls.Count();
            welcomeInfo.Server_MachineName = Server.MachineName;
            welcomeInfo.Server_IP = Request.ServerVariables["LOCAL_ADDR"];
            welcomeInfo.Server_Port = Request.ServerVariables["SERVER_PORT"];
            welcomeInfo.Server_Environment = String.Format(".NET CLR {0}.{1}.{2}.{3}", Environment.Version.Major, Environment.Version.Minor, Environment.Version.Build, Environment.Version.Revision);
            welcomeInfo.Server_OS = Environment.OSVersion.ToString();
            welcomeInfo.Server_IIS = Request.ServerVariables["SERVER_SOFTWARE"];
            return View(welcomeInfo);
        }

        //
        // GET: /SiteConfig/Details/
        public ActionResult Details()
        {
            var config = db.Sysconfigs.FirstOrDefault();
            var player = new List<string>(){
                "WebPlayer9",
                "Qvod"
            };
            ViewBag.Player = new SelectList(player,null,null,config.Player);
            return View(config);
        }

        // 修改系统配置
        // POST: /SiteConfig/Details
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Details(SysConfig sysConfig)
        {
            //如果验证通过
            if (ModelState.IsValid)
            {
                db.Entry(sysConfig).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Details");
            }
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
